package model.admin;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.opensymphony.xwork2.Result;

import myutil.Connect;

import vo.BatchDetailsVo;

public class ModifyBatchDetailsMo {

	

	public int addBatchDetails(BatchDetailsVo newBatch, String degree_name,String type) {

		String query = "INSERT INTO batch_details " + "(batch_name,"
				+ " start_date, " + "end_date, " + "student_count, "
				+ "start_year, " + "incharge_id, " + "description,status)"
				+ " VALUES ('" + newBatch.getBatch_name() + "'," + " '"
				+ newBatch.getStart_date() + "', " + "'"
				+ newBatch.getEnd_date() + "'," + ""
				+ newBatch.getStudent_count() + "," + ""
				+ newBatch.getStart_year() + ", " + ""
				+ newBatch.getIncharge_id() + "," + " '"
				+ newBatch.getDescription() + "',1);";
		int result;
		String query1= "select max(batch_id) as batch_id from batch_details;";
		int batch_id=0;
		Connect c = new Connect();
		try {
			c.dml(query);
			c.logger(type, "Insert");
			ResultSet r= c.getResult(query1);
			if(r.next())
				batch_id=r.getInt("batch_id");
			query1="INSERT INTO degree_management (degree_id,degree_incharge_id, batch_id, incharge_start_date, incharge_end_date, description) VALUES " +
					"((select degree_id from degree_details where degree_name='"+degree_name+"')," +
					"(select staff_id from college_staff where staff_name='"+
					newBatch.getIncharge_name()+"')," +
					batch_id+",'" +
					newBatch.getIncharge_start_date()+"' ,'" +
					newBatch.getIncharge_end_date()+"', '" +
					newBatch.getDescription()+"');";
			System.out.println(query1);
			c.dml(query1);
			
			result = 1;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = -1;
		}
		return result;
	}

	public void editBatchDetails(int batch_id,String batch_name,String start_date,
	String end_date,int student_count,int start_year,int incharge_id,String description,String type) {
		
		String query="update batch_details set batch_name='"+batch_name+"',start_date='"+start_date+"',end_date='"+end_date+"',"
				+"student_count="+student_count+",start_year="+start_year+",incharge_id="+incharge_id+",description='"+description+"' where batch_id="+batch_id+"";
		System.out.println(query);
		Connect c= new Connect();
		try {
			c.dml(query);
			c.logger(type, "Update");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void deleteBatchDetails(int batch_id,String type) {
		String query="update batch_details set status='Inactive' where batch_id="+batch_id+";";
		System.out.println(query);
		int result;
		Connect c = new Connect();
		try {
			c.dml(query);
			c.logger(type, "Update");
			result = 1;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = -1;
		}
	}
	
	
	
	// Function to add a Degree to the batch
	public void addDegreetoBatch(int degree_id,int batch_id,String type){
		String query, query1,query2,q3,q4;
		
		query="insert into degree_management(degree_id,batch_id,incharge_start_date,status) values("+degree_id+" ,"+batch_id+" ,current_date(),'Active')";
		System.out.println();
		query1="SELECT DISTINCT department_id, description FROM degree_department_mgmt WHERE degree_id ="+degree_id;
		
		Connect c= new Connect();
		try {
			c.dml(query);
			
			ResultSet rs= c.getResult(query1);
			while(rs.next()){
				int dept_id=0;
				String desc="";
				dept_id=rs.getInt("department_id");
				desc=rs.getString("description");
				query2="insert into degree_department(department_id,degree_id,batch_id,description,status) values("+dept_id+" , "+degree_id+" ,"+batch_id+" , '"+desc+"' ,'Active')";
				System.out.println(query2);
				c.dml(query2);
				c.logger(type, "Insert");
				q3="select degree_course_id,course_id,description,mandatory from degree_course where status='Active' and degree_id="+degree_id+" and department_id="+dept_id;
					ResultSet rs1= c.getResult(q3);
					while(rs1.next()){
						int degree_course_id=0;
						int course=0;
						String descr="";
						String man="";
						degree_course_id = rs1.getInt("degree_course_id");
						course=rs1.getInt("course_id");
						descr=rs1.getString("description");
						man=rs1.getString("mandatory");
						q4="insert into course_management(course_id,batch_id,mandatory,description,course_code,degree_course_id,status) values("+course+" ,"+batch_id+", '"+man+"' ,'"+descr+"' , '"+descr+"' ,"+degree_course_id+" ,'Active')";
						c.dml(q4);
						c.logger(type, "Insert");
					}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
